package com.chencs.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

public class PidUtils {

    static  String cookie = "t=3d3818ef80d48c4da3447fa55d74c70f; account-path-guide-s1=true; 16510629_yxjh-filter-1=true; undefined_yxjh-filter-1=true; cna=wERrEvs2+G8CAXPGZHtq8mGq; excel-down-time-MKT_HOT_EXCEL_LIST=1510033187773; 129969440_yxjh-filter-1=true; qq-best-goods-down-time=1528335249498; cookie2=1b479ee1588a1cb64b19f5e737fc1c8c; v=0; _tb_token_=bb97fa7446a8; alimamapwag=TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgNi4xOyBXT1c2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzY3LjAuMzM5Ni44NyBTYWZhcmkvNTM3LjM2; cookie32=a3888a144f863985b889e5f4ed1edbc8; alimamapw=XkxdAwAJB1AHXTwHUAAFAFlWDwZXAAYIAAJTVFYEUlcCVAlWCVABBFVUUA%3D%3D; cookie31=MTY1MTA2MjksZnpoNjE4NTEyOSxmemg2MTg1MTI5QDEyNi5jb20sVEI%3D; pub-message-center=1; login=UtASsssmOIJ0bQ%3D%3D; rurl=aHR0cDovL3B1Yi5hbGltYW1hLmNvbS8%2Fc3BtPWEyMTl0Ljc5MDAyMjEuMTk5ODkxMDQxOS5kODQxYjQ2OGIuMTgxYjc1YTVDbmo2NGc%3D; apush6d83509f58cd1771638bcb989c91cf96=%7B%22ts%22%3A1529637225212%2C%22heir%22%3A1529637166834%2C%22parentId%22%3A1529637165568%7D; isg=BGRkxvpz4_uTeBX-vhteQksKNWKWVYn4z72-DX6BQS-0KQPzpwsO9tDP7YEx8cC_";

    public  static   List<PidBean> getPid(int pageNo){
        try {
            org.jsoup.nodes.Document res = Jsoup.connect("http://pub.alimama.com/common/adzone/adzoneManage.json?spm=a219t.7900221%2F1.1998910419.db9f5f632.3d7c75a5E7M3oo&tab=3&toPage="+pageNo+"&perPageSize=40&gcid=8&t=1529386676333&pvid=60_115.199.154.147_2576_1529386842043&_tb_token_=eed1e3e163683&_input_charset=utf-8")
                    .timeout(10000).cookie("Cookie",cookie).ignoreContentType(true).get();
            String  str = res.body().text();

            JSONObject jsonObject = JSON.parseObject(str);
            JSONObject jsonObject2 = JSON.parseObject(jsonObject.getString("data"));
//         System.out.println(jsonObject2.get("pagelist"));

            List<PidBean> pidBean = JSONObject.parseArray(jsonObject2.get("pagelist").toString(),PidBean.class);
            return pidBean;
//            System.out.println(pidBean.size());
//            for(int i=0;i<pidBean.size();i++){
//                System.out.println(pidBean.get(i).getName()+"-"+pidBean.get(i).getAdzonePid());
//            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }




    public static void main(String[] args) {
        String urlPath = "http://pub.alimama.com/common/adzone/adzoneManage.json?spm=a219t.7900221%2F1.1998910419.db9f5f632.3d7c75a5E7M3oo&tab=3&toPage=1&perPageSize=40&gcid=8&t=1529386676333&pvid=60_115.199.154.147_2576_1529386842043&_tb_token_=eed1e3e163683&_input_charset=utf-8";
         String cookie = "t=3d3818ef80d48c4da3447fa55d74c70f; account-path-guide-s1=true; 16510629_yxjh-filter-1=true; undefined_yxjh-filter-1=true; cna=wERrEvs2+G8CAXPGZHtq8mGq; excel-down-time-MKT_HOT_EXCEL_LIST=1510033187773; 129969440_yxjh-filter-1=true; qq-best-goods-down-time=1528335249498; cookie2=1b479ee1588a1cb64b19f5e737fc1c8c; v=0; _tb_token_=bb97fa7446a8; alimamapwag=TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgNi4xOyBXT1c2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzY3LjAuMzM5Ni44NyBTYWZhcmkvNTM3LjM2; cookie32=a3888a144f863985b889e5f4ed1edbc8; alimamapw=XkxdAwAJB1AHXTwHUAAFAFlWDwZXAAYIAAJTVFYEUlcCVAlWCVABBFVUUA%3D%3D; cookie31=MTY1MTA2MjksZnpoNjE4NTEyOSxmemg2MTg1MTI5QDEyNi5jb20sVEI%3D; pub-message-center=1; login=Vq8l%2BKCLz3%2F65A%3D%3D; rurl=aHR0cDovL3B1Yi5hbGltYW1hLmNvbS8%3D; apush6d83509f58cd1771638bcb989c91cf96=%7B%22ts%22%3A1529559847480%2C%22parentId%22%3A1529559834446%7D; isg=BFBQEMwAP_g9yuEyan8yxsdmIZ5isTX802EqwUognqtohfkv8y9U813zWU1lVew7";
         //        try {
//            URL url = new URL(urlPath);
//            // 下载网络文件
//            int bytesum = 0;
//            int byteread = 0;
//            URLConnection conn = url.openConnection();
//            conn.setRequestProperty("Cookie", cookie);
//            conn.setDoInput(true);
//            InputStream inStream = conn.getInputStream();
//            byte[] buffer = new byte[1204];
//            int length;
//            while ((byteread = inStream.read(buffer)) != -1) {
//                bytesum += byteread;
//            }
//            System.out.println();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//            System.out.println("读取订单失败!");
//        }


        for(int i =450;i<600;i++){
                try {
                    Thread.currentThread().sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            List<PidBean> pidBean =  PidUtils.getPid(i);
              for(int x=0;x<pidBean.size();x++){
                System.out.println(pidBean.get(x).getName()+"-"+pidBean.get(x).getAdzonePid());
            }
        }
    }


}
